home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16.lha / Python-1.6 / Lib / Python1.6 / exceptions.py < prev    next >
Encoding:
Python Source  |  2000-03-11  |  6.7 KB  |  248 lines

  1. """Class based built-in exception hierarchy.
  2.  
  3. New with Python 1.5, all standard built-in exceptions are now class objects by
  4. default.  This gives Python's exception handling mechanism a more
  5. object-oriented feel.  Traditionally they were string objects.  Python will
  6. fallback to string based exceptions if the interpreter is invoked with the -X
  7. option, or if some failure occurs during class exception initialization (in
  8. this case a warning will be printed).
  9.  
  10. Most existing code should continue to work with class based exceptions.  Some
  11. tricky uses of IOError may break, but the most common uses should work.
  12.  
  13. Here is a rundown of the class hierarchy.  You can change this by editing this
  14. file, but it isn't recommended because the old string based exceptions won't
  15. be kept in sync.  The class names described here are expected to be found by
  16. the bltinmodule.c file.  If you add classes here, you must modify
  17. bltinmodule.c or the exceptions won't be available in the __builtin__ module,
  18. nor will they be accessible from C.
  19.  
  20. The classes with a `*' are new since Python 1.5.  They are defined as tuples
  21. containing the derived exceptions when string-based exceptions are used.  If
  22. you define your own class based exceptions, they should be derived from
  23. Exception.
  24.  
  25. Exception(*)
  26.  |
  27.  +-- SystemExit
  28.  +-- StandardError(*)
  29.       |
  30.       +-- KeyboardInterrupt
  31.       +-- ImportError
  32.       +-- EnvironmentError(*)
  33.       |    |
  34.       |    +-- IOError
  35.       |    +-- OSError(*)
  36.       |         |
  37.       |         +-- WindowsError(*)
  38.       |
  39.       +-- EOFError
  40.       +-- RuntimeError
  41.       |    |
  42.       |    +-- NotImplementedError(*)
  43.       |
  44.       +-- NameError
  45.       |    |
  46.       |    +-- UnboundLocalError(*)
  47.       |
  48.       +-- AttributeError
  49.       +-- SyntaxError
  50.       +-- TypeError
  51.       +-- AssertionError
  52.       +-- LookupError(*)
  53.       |    |
  54.       |    +-- IndexError
  55.       |    +-- KeyError
  56.       |
  57.       +-- ArithmeticError(*)
  58.       |    |
  59.       |    +-- OverflowError
  60.       |    +-- ZeroDivisionError
  61.       |    +-- FloatingPointError
  62.       |
  63.       +-- ValueError
  64.       |    |
  65.       |    +-- UnicodeError(*)
  66.       |
  67.       +-- SystemError
  68.       +-- MemoryError
  69. """
  70.  
  71. class Exception:
  72.     """Proposed base class for all exceptions."""
  73.     def __init__(self, *args):
  74.         self.args = args
  75.  
  76.     def __str__(self):
  77.         if not self.args:
  78.             return ''
  79.         elif len(self.args) == 1:
  80.             return str(self.args[0])
  81.         else:
  82.             return str(self.args)
  83.  
  84.     def __getitem__(self, i):
  85.         return self.args[i]
  86.  
  87. class StandardError(Exception):
  88.     """Base class for all standard Python exceptions."""
  89.     pass
  90.  
  91. class SyntaxError(StandardError):
  92.     """Invalid syntax."""
  93.     filename = lineno = offset = text = None
  94.     msg = ""
  95.     def __init__(self, *args):
  96.         self.args = args
  97.         if len(self.args) >= 1:
  98.             self.msg = self.args[0]
  99.         if len(self.args) == 2:
  100.             info = self.args[1]
  101.             try:
  102.                 self.filename, self.lineno, self.offset, self.text = info
  103.             except:
  104.                 pass
  105.     def __str__(self):
  106.         return str(self.msg)
  107.  
  108. class EnvironmentError(StandardError):
  109.     """Base class for I/O related errors."""
  110.     def __init__(self, *args):
  111.         self.args = args
  112.         self.errno = None
  113.         self.strerror = None
  114.         self.filename = None
  115.         if len(args) == 3:
  116.             # open() errors give third argument which is the filename.  BUT,
  117.             # so common in-place unpacking doesn't break, e.g.:
  118.             #
  119.             # except IOError, (errno, strerror):
  120.             #
  121.             # we hack args so that it only contains two items.  This also
  122.             # means we need our own __str__() which prints out the filename
  123.             # when it was supplied.
  124.             self.errno, self.strerror, self.filename = args
  125.             self.args = args[0:2]
  126.         if len(args) == 2:
  127.             # common case: PyErr_SetFromErrno()
  128.             self.errno, self.strerror = args
  129.  
  130.     def __str__(self):
  131.         if self.filename is not None:
  132.             return '[Errno %s] %s: %s' % (self.errno, self.strerror,
  133.                                           repr(self.filename))
  134.         elif self.errno and self.strerror:
  135.             return '[Errno %s] %s' % (self.errno, self.strerror)
  136.         else:
  137.             return StandardError.__str__(self)
  138.  
  139. class IOError(EnvironmentError):
  140.     """I/O operation failed."""
  141.     pass
  142.  
  143. class OSError(EnvironmentError):
  144.     """OS system call failed."""
  145.     pass
  146.  
  147. class WindowsError(OSError):
  148.     """MS-Windows OS system call failed."""
  149.     pass
  150.  
  151. class RuntimeError(StandardError):
  152.     """Unspecified run-time error."""
  153.     pass
  154.  
  155. class NotImplementedError(RuntimeError):
  156.     """Method or function hasn't been implemented yet."""
  157.     pass
  158.  
  159. class SystemError(StandardError):
  160.     """Internal error in the Python interpreter.
  161.  
  162.     Please report this to the Python maintainer, along with the traceback,
  163.     the Python version, and the hardware/OS platform and version."""
  164.     pass
  165.  
  166. class EOFError(StandardError):
  167.     """Read beyond end of file."""
  168.     pass
  169.  
  170. class ImportError(StandardError):
  171.     """Import can't find module, or can't find name in module."""
  172.     pass
  173.  
  174. class TypeError(StandardError):
  175.     """Inappropriate argument type."""
  176.     pass
  177.  
  178. class ValueError(StandardError):
  179.     """Inappropriate argument value (of correct type)."""
  180.     pass
  181.  
  182. class KeyboardInterrupt(StandardError):
  183.     """Program interrupted by user."""
  184.     pass
  185.  
  186. class AssertionError(StandardError):
  187.     """Assertion failed."""
  188.     pass
  189.  
  190. class ArithmeticError(StandardError):
  191.     """Base class for arithmetic errors."""
  192.     pass
  193.  
  194. class OverflowError(ArithmeticError):
  195.     """Result too large to be represented."""
  196.     pass
  197.  
  198. class FloatingPointError(ArithmeticError):
  199.     """Floating point operation failed."""
  200.     pass
  201.  
  202. class ZeroDivisionError(ArithmeticError):
  203.     """Second argument to a division or modulo operation was zero."""
  204.     pass
  205.  
  206. class LookupError(StandardError):
  207.     """Base class for lookup errors."""
  208.     pass
  209.  
  210. class IndexError(LookupError):
  211.     """Sequence index out of range."""
  212.     pass
  213.  
  214. class KeyError(LookupError):
  215.     """Mapping key not found."""
  216.     pass
  217.  
  218. class AttributeError(StandardError):
  219.     """Attribute not found."""
  220.     pass
  221.  
  222. class NameError(StandardError):
  223.     """Name not found globally."""
  224.     pass
  225.  
  226. class UnboundLocalError(NameError):
  227.     """Local name referenced but not bound to a value."""
  228.     pass
  229.  
  230. class UnicodeError(ValueError):
  231.     """Unicode related error."""
  232.     pass
  233.  
  234. class MemoryError(StandardError):
  235.     """Out of memory."""
  236.     pass
  237.  
  238. class SystemExit(Exception):
  239.     """Request to exit from the interpreter."""
  240.     def __init__(self, *args):
  241.         self.args = args
  242.         if len(args) == 0:
  243.             self.code = None
  244.         elif len(args) == 1:
  245.             self.code = args[0]
  246.         else:
  247.             self.code = args
  248.